/* transmite_server的测试客户端实现*/

#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <brpc/channel.h>
#include <functional>
#include <cctype>
#include "logger.hpp"
#include "brpc.hpp"
#include "etcd.hpp"
#include "transmite.pb.h"
#include "utils.hpp"


DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "调用的服务目录");

ChatSystem::ServiceManager::ptr service_manager;

void string_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    auto channel = service_manager->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    ChatSystem::MsgTransmiteService_Stub stub(channel.get());
    ChatSystem::NewMessageReq req;
    ChatSystem::GetTransmiteTargetRsp rsp;
    req.set_request_id(ChatSystem::Util::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(ChatSystem::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);
    brpc::Controller cntl;
    stub.GetTransmiteTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}
void image_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    auto channel = service_manager->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    ChatSystem::MsgTransmiteService_Stub stub(channel.get());
    ChatSystem::NewMessageReq req;
    ChatSystem::GetTransmiteTargetRsp rsp;
    req.set_request_id(ChatSystem::Util::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(ChatSystem::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_contents(msg);
    brpc::Controller cntl;
    stub.GetTransmiteTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void speech_message(const std::string &uid, const std::string &sid, const std::string &msg) {
    auto channel = service_manager->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    ChatSystem::MsgTransmiteService_Stub stub(channel.get());
    ChatSystem::NewMessageReq req;
    ChatSystem::GetTransmiteTargetRsp rsp;
    req.set_request_id(ChatSystem::Util::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(ChatSystem::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_speech_contents(msg);
    brpc::Controller cntl;
    stub.GetTransmiteTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void file_message(const std::string &uid, const std::string &sid, 
    const std::string &filename, const std::string &content) {
    auto channel = service_manager->choose(FLAGS_transmite_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    ChatSystem::MsgTransmiteService_Stub stub(channel.get());
    ChatSystem::NewMessageReq req;
    ChatSystem::GetTransmiteTargetRsp rsp;
    req.set_request_id(ChatSystem::Util::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(ChatSystem::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_name(filename);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());
    brpc::Controller cntl;
    stub.GetTransmiteTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}


int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    ChatSystem::InitLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    //1. 先去注册中心进行服务的发现---发现speech_server的服务器节点地址信息并实例化通信信道
    //1.1 先构造Rpc整体的服务管理信道类
    service_manager = std::make_shared<ChatSystem::ServiceManager>();
    service_manager->declared(FLAGS_transmite_service);  //设置需要关心的服务
    auto put_cb = std::bind(&ChatSystem::ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ChatSystem::ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    //1.2 构造服务发现对象 
    ChatSystem::Discovery::ptr dclient = std::make_shared<ChatSystem::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    
    // //1.3 通过Rpc信道管理对象，获取提供Speech服务的信道
    // auto channel = service_manager->choose(FLAGS_transmite_service);
    // if(!channel)
    // {
    //     std::this_thread::sleep_for(std::chrono::seconds(1));
    //     return -1;
    // }
    // //4. 发起Speech Rpc调用
    // // 2.实例化pb中的SpeechService_Stub类对象；
    // ChatSystem::MsgTransmiteService_Stub stub(channel.get());
    // // 3.发起rpc请求，获取响应进行处理；
    // brpc::Controller cntl;
    // ChatSystem::NewMessageReq req;
    // ChatSystem::GetTransmiteTargetRsp rsp;
    // req.set_request_id("7777");
    // req.set_user_id("2776-109f8f05-0000");
    // req.set_chat_session_id("会话ID1");
    // req.mutable_message()->set_message_type(ChatSystem::MessageType::STRING);
    // req.mutable_message()->mutable_string_message()->set_content("测试消息内容");


    // stub.GetTransmiteTarget(&cntl, &req, &rsp, nullptr);   //rpc 同步调用
    // if(cntl.Failed() == true)
    // {
    //     std::cout << "Rpc 调用失败！" << cntl.ErrorText() << std::endl;
 
    //     std::this_thread::sleep_for(std::chrono::seconds(1));
    //     return -1;
    // }
    // if(rsp.success() == false)
    // {
    //     std::cout << rsp.errmsg() << std::endl;
    //     return -1;
    // }
    // std::cout << "收到的响应ID: " << rsp.request_id() << std::endl;
    // std::cout << "msg_id: " << rsp.message().message_id() << std::endl;
    // std::cout << "chat_session_id: " << rsp.message().chat_session_id() << std::endl;
    // std::cout << "timestamp: " << rsp.message().timestamp() << std::endl;
    // std::cout << "sender: " << rsp.message().sender().user_id() << std::endl;
    // std::cout << "message: " << rsp.message().message().string_message().content() << std::endl;

    // for(int i = 0; i < rsp.target_id_list().size(); ++i)
    // {
    //     std::cout << "target: " << rsp.target_id_list(i) << std::endl;
    // }

    // string_message("uid1", "会话ID1", "吃饭了吗？");
    // string_message("uid2", "会话ID1", "吃的盖浇饭！！");
    // image_message("uid1", "会话ID1", "可爱表情图片数据");
    // speech_message("uid2", "会话ID1", "动听猪叫声数据");
    file_message("e1c3-abc29d99-0000", "1379-15db9a3c-0005", "猪爸爸的文件名称", "猪爸爸的文件数据");
    file_message("e1c3-abc29d99-0000", "ec24-ed18497c-0003", "给猪妈妈的情书", "美丽的猪妈妈，爱你，么么哒！");


}

